package com.skt.common_hr.common.components
{
	import com.skt.common_hr.common.Variables;
	import com.wcl.utils.StringUtil;
	
	import flash.display.Sprite;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	
	[ResourceBundle("CommonHr")]
	public class CommonAlert extends UIComponent
	{
		public static const TITLE_INFO:String  = "ALERT_TITLE_INFO";
		public static const TITLE_ERROR:String = "TITLE_ERROR";
		
		private static const COMMON_BUNDLE:String = "CommonHr";
		
		static public function getInstance():CommonAlert
		{
			return new CommonAlert;
		}
		
		static public function showMessage(bundle:String, msg:String, params:Array=null, title:String = TITLE_INFO, parent:Sprite=null, closeHandler:Function=null):void 
		{
			CommonAlert.getInstance().showMessage_i(bundle, msg, params, title, parent, closeHandler);
		}
		
		static public function showMsgYesNo(bundle:String, msg:String, params:Array, title:String, parent:Sprite, 
											yesFunction:Function, noFunction:Function, 
											 Yeslabel:String = "YES", Nolabel:String = "NO"):void 
		{
			CommonAlert.getInstance().showMsgYesNo_i(bundle, msg, params, title, parent, yesFunction, noFunction, Yeslabel, Nolabel);
		}



		public function showMessage_i(bundle:String, msg_key:String, params:Array, title_id:String, parent:Sprite, closeHandler:Function=null):void
		{
			var title:String = resourceManager.getString(COMMON_BUNDLE, title_id); 
			if(StringUtil.NVL(title) == "") title = title_id;
			var msg:String = resourceManager.getString(bundle, msg_key);
			//trace(module + ", " + AppModuleManager.getModuleID(module));
			var newAlert:Alert = Alert.show( buildStatusString((msg==null) ? msg_key : msg, params), 
											title, 
											Alert.OK, 
											parent, 
											function(event:CloseEvent):void {if(closeHandler!=null) closeHandler(); }
					                );
			newAlert.width = 300;
			newAlert.height = 120; 
		}
		
		
		private function buildStatusString(src:String, param:Array):String
		{
			if(src==null)
				return "";
			while(src.search("%p")>= 0) {
				var p:* = param.shift();
				src = src.replace("%p", (p==undefined)? "" : p);
			} 
			return src;
		}
		
		/**
         * Alert.show 재 정의
         */
		public function showMsgYesNo_i(bundle:String, msg_key:String, params:Array, title_id:String, parent:Sprite, yesFunction:Function, noFunction:Function, 
											 Yeslabel:String = "YES", Nolabel:String = "NO"): void
        {
        	var iconImage :Class = null;
        	var defaultTitle:String ="확인";
			var title:String;
			
			if (title_id == "") {
				title = defaultTitle ;
			}
			else {
				title = resourceManager.getString(COMMON_BUNDLE, title_id);
			}
			
			var msg:String = resourceManager.getString(bundle, msg_key);
			//trace(module+ ", " + msg_key+","+msg);
			Alert.yesLabel = resourceManager.getString(COMMON_BUNDLE, Yeslabel);
			Alert.noLabel = resourceManager.getString(COMMON_BUNDLE, Nolabel);
			
            var newAlert:Alert = Alert.show(buildStatusString((msg==null) ? msg_key : msg, params), title, Alert.YES|Alert.NO, parent,
					                function(event:CloseEvent): void {
					                    if (event.detail == Alert.YES) {
					                        if (yesFunction != null) yesFunction();
					                    } else {
					                        if (noFunction != null) noFunction();
					                    }
					                },iconImage, Alert.NO
					            );
			newAlert.width = 300;
			newAlert.height = 120;		            
        }
		
		
		
		override protected function initializationComplete():void
		{
			resourceManager.localeChain = [Variables.current_locale];
		}
		
		
	}
}